package com.ssy.lingxi.system.service.impl;

import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.system.dao.MemberConfigAppraisalItemRepository;
import com.ssy.lingxi.system.dto.response.MemberConfigAppraisalItemPageResponse;
import com.ssy.lingxi.system.entity.MemberConfigAppraisalItem;
import com.ssy.lingxi.system.model.dto.common.CommonStateDTO;
import com.ssy.lingxi.system.service.MemberConfigAppraisalItemService;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 会员考评项目设置服务实现类
 * @author yzc
 * @version 2.0.0
 * @date 2021/5/17
 */
@Service
public class MemberConfigAppraisalItemServiceImpl implements MemberConfigAppraisalItemService {

    @Resource
    private MemberConfigAppraisalItemRepository memberConfigAppraisalItemRepository;

    @Override
    public Wrapper<List<MemberConfigAppraisalItemPageResponse>> listMemberConfigAppraisalItem() {
        List<MemberConfigAppraisalItem> list = memberConfigAppraisalItemRepository.findAll(Sort.by("id"));

        List<MemberConfigAppraisalItemPageResponse> resultList = list.stream().map(e -> {
            MemberConfigAppraisalItemPageResponse response = new MemberConfigAppraisalItemPageResponse();
            response.setId(e.getId());
            response.setName(e.getName());
            response.setTypeDesc(e.getTypeDesc());
            response.setRoleName(e.getRole().getName());
            response.setState(e.getState());
            return response;
        }).collect(Collectors.toList());


        return Wrapper.success(resultList);

    }

    @Override
    public Wrapper<Void> enableMemberConfigAppraisalItem(CommonStateDTO stateDTO) {
        MemberConfigAppraisalItem memberConfigAppraisalItem = memberConfigAppraisalItemRepository.findById(stateDTO.getId()).orElse(null);
        if (Objects.isNull(memberConfigAppraisalItem)) {
            return Wrapper.fail(ResponseCode.SYSTEM_RECORDS_DON_T_EXIST);
        }

        memberConfigAppraisalItem.setState(stateDTO.getState());
        memberConfigAppraisalItemRepository.saveAndFlush(memberConfigAppraisalItem);

        return Wrapper.success();
    }
}
